#include "LightWidget.h"
#include "ui_LightWidget.h"

#include "Light.h"


namespace
{
    const float fac = 1.f/255.f;
}


LightWidget::LightWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LightWidget)
{
    ui->setupUi(this);
    setEnabled(false);

    m_light = NULL;
}

LightWidget::~LightWidget()
{
    delete ui;
}

void LightWidget::setLight(Light *light)
{
    m_light = light;
    if(m_light)
    {
        updateForm();
        setEnabled(true);
    }
    else
        setEnabled(false);
}

void LightWidget::updateLight()
{
    m_light->setPosition(getPosition());

    emit updated();
}

void LightWidget::updateDiffuse()
{
    Vector3f diffuseFloat = getDiffuse();
    m_light->setDiffuse(diffuseFloat);

    Vector3i diffuseInt = (Vector3i)(diffuseFloat * 255);
    ui->diffusePreview->setStyleSheet(
                QString("background-color: rgb( %1, %2, %3);").arg(diffuseInt.x).arg(diffuseInt.y).arg(diffuseInt.z) );

    emit updated();
}

void LightWidget::updateSpecular()
{
    Vector3f specularFloat = getSpecular();
    m_light->setSpecular(specularFloat);

    Vector3i specularInt = (Vector3i)(specularFloat * 255);
    ui->specularPreview->setStyleSheet(
                QString("background-color: rgb( %1, %2, %3);").arg(specularInt.x).arg(specularInt.y).arg(specularInt.z) );

    emit updated();
}

void LightWidget::updateForm()
{
    setPosition(m_light->getPosition());
    setDiffuse(m_light->getDiffuse());
    setSpecular(m_light->getSpecular());

    Vector3i specularInt = (Vector3i)(m_light->getSpecular() * 255);
    ui->specularPreview->setStyleSheet(
                QString("background-color: rgb( %1, %2, %3);").arg(specularInt.x).arg(specularInt.y).arg(specularInt.z) );

    Vector3i diffuseInt = (Vector3i)(m_light->getDiffuse() * 255);
    ui->diffusePreview->setStyleSheet(
                QString("background-color: rgb( %1, %2, %3);").arg(diffuseInt.x).arg(diffuseInt.y).arg(diffuseInt.z) );
}

void LightWidget::setPosition(const Point3f &pos)
{
    ui->x_spinBox->blockSignals(true);
    ui->y_spinBox->blockSignals(true);
    ui->z_spinBox->blockSignals(true);

    ui->x_spinBox->setValue(pos.x);
    ui->y_spinBox->setValue(pos.y);
    ui->z_spinBox->setValue(pos.z);

    ui->x_spinBox->blockSignals(false);
    ui->y_spinBox->blockSignals(false);
    ui->z_spinBox->blockSignals(false);
}

void LightWidget::setDiffuse(const Vector3f &diff)
{
    ui->diffuseR_spinBox->blockSignals(true);
    ui->diffuseG_spinBox->blockSignals(true);
    ui->diffuseB_spinBox->blockSignals(true);

    ui->diffuseR_slider->setValue(diff.x*255);
    ui->diffuseG_slider->setValue(diff.y*255);
    ui->diffuseB_slider->setValue(diff.z*255);

    ui->diffuseR_spinBox->blockSignals(false);
    ui->diffuseG_spinBox->blockSignals(false);
    ui->diffuseB_spinBox->blockSignals(false);
}

void LightWidget::setSpecular(const Vector3f &spec)
{
    ui->specularR_spinBox->blockSignals(true);
    ui->specularG_spinBox->blockSignals(true);
    ui->specularB_spinBox->blockSignals(true);

    ui->specularR_slider->setValue(spec.x*255);
    ui->specularG_slider->setValue(spec.y*255);
    ui->specularB_slider->setValue(spec.z*255);

    ui->specularR_spinBox->blockSignals(false);
    ui->specularG_spinBox->blockSignals(false);
    ui->specularB_spinBox->blockSignals(false);
}

Point3f LightWidget::getPosition()
{
    return Point3f(ui->x_spinBox->value(), ui->y_spinBox->value(), ui->z_spinBox->value());
}

Vector3f LightWidget::getDiffuse()
{
    return Vector3f(ui->diffuseR_spinBox->value()*fac, ui->diffuseG_spinBox->value()*fac, ui->diffuseB_spinBox->value()*fac);
}

Vector3f LightWidget::getSpecular()
{
    return Vector3f(ui->specularR_spinBox->value()*fac, ui->specularG_spinBox->value()*fac, ui->specularB_spinBox->value()*fac);
}
